﻿using PayrollHR.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PayrollHR.Administration.Modules
{
    public partial class DevicesControl : BaseAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Bind();
            }
        }
        #region Function
        private void Bind(string result = null)
        {
            string DeviceCode = null;
            string DeviceName = null;
            if (result == null)
            {
                gvDevicess.DataSource = this.DeviceService.GetAllDevice(deviceCode: DeviceCode, deviceName: DeviceName, pageSize: 100);
            }
            else {
                gvDevicess.DataSource = this.DeviceService.Delete(deviceId: result, deviceCode: DeviceCode, deviceName: DeviceName, pageSize: 100);
            }
            gvDevicess.DataBind();
        }
        protected void Delete()
        {
            if (Page.IsValid)
            {
                int Row = gvDevicess.Rows.Count;
                string result = "";
                try
                {
                    foreach (GridViewRow row in gvDevicess.Rows)
                    {
                        var cbResultsId = row.FindControl("cbDevices") as CheckBox;
                        bool isChecked = cbResultsId.Checked;
                        if (isChecked)
                        {
                            result += gvDevicess.DataKeys[row.RowIndex].Value + ",";
                        }
                    }
                    if (result.Length > 0)
                    {
                        result = result.Substring(0, result.Length - 1);
                        this.Bind(result);
                    }
                }
                catch (Exception ex)
                {
                    ProcessException(ex);
                }
            }
        }
        #endregion

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.Delete();
        }

        protected void gvDevicess_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDevicess.PageIndex = e.NewPageIndex;
            this.Bind();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            this.Bind();
        }
    }
}